package com.xxl.job.consume.service;


import com.xxl.job.consume.kafka.KafkaProduceService;
import com.xxl.job.consume.model.DataMonitorAlarmModel;
import com.xxl.job.consume.model.JsonBkInterfaceModel;
import com.xxl.job.consume.model.JsonHeaderModel;
import com.xxl.job.core.log.XxlJobLogger;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

/**
 * @author: liu.ziyang
 * @date: 2021/3/29 11:09
 * @description: 发博坤接口服务方法
 */
@Service
public class BkJsonDataService {


    //实时监测数据
    public static String MSG_DEV_TYPE ="device_state";//实时监测数据
    public static String  MSG_ALARM_TYPE ="device_warning";//监测报警数据
    public static String PIPE_GALLERY_CODE ="GL-107901-1962";
    public static String PIPE_GALLERY_NAME = "黑龙江路管廊";

    public static String KDGC_SENDER = "黑龙江路综合管廊管控平台";
    public static String BK_RECEVIER = "BIM";
    public static String EMPTY_LENGTH = "";
    @Autowired
    private KafkaProduceService kafkaProduceService;


    //发送监测报警数据
    public void sendAlarmMessage(String devCode, String devName,String SignalCode, String WarningDec , String alarmValue, String type,String warninTime){
        DataMonitorAlarmModel dataMonitorAlarmModel = getDataMonitorAlarmModel(devCode,devName, SignalCode,  WarningDec , alarmValue, type, warninTime);
        JsonHeaderModel jsonHeaderModel =  getJsonHeaderModel(MSG_ALARM_TYPE);
        JsonBkInterfaceModel jsonBkInterfaceModel = getJsonBkInterfaceModel(jsonHeaderModel,dataMonitorAlarmModel);
        try {
            this.kafkaProduceService.sendBkAlarmMessage(jsonBkInterfaceModel);
        } catch (Exception e) {
            XxlJobLogger.log("发送告警信息失败:"+jsonBkInterfaceModel.toString(), e);
        }
    }


    //公共消息头部分 消息组装
    private JsonHeaderModel getJsonHeaderModel(String msgType){
        //头实体
        JsonHeaderModel jsonHeaderModel2 = new JsonHeaderModel();
        //头组装
        jsonHeaderModel2.setMsgID(getUUID());
        jsonHeaderModel2.setMsgType(msgType);
        jsonHeaderModel2.setSender(KDGC_SENDER);
        jsonHeaderModel2.setSendTime(getSystemTimeStampToDate());
        jsonHeaderModel2.setRecevier(BK_RECEVIER);
        jsonHeaderModel2.setLength(EMPTY_LENGTH);
        return jsonHeaderModel2;
    }



    //监测报警数据 消息组装
    private DataMonitorAlarmModel getDataMonitorAlarmModel(String devCode, String devName,String SignalCode, String WarningDec , String alarmValue, String type,String warninTime){
        DataMonitorAlarmModel dataMonitorAlarmModel = new DataMonitorAlarmModel();
        //告警组装
        dataMonitorAlarmModel.setPipeGalleryCode(PIPE_GALLERY_CODE);
        dataMonitorAlarmModel.setPipeGalleryMame(PIPE_GALLERY_NAME);
        dataMonitorAlarmModel.setDeviceCode(devCode);
        dataMonitorAlarmModel.setDeviceName(devName);
        dataMonitorAlarmModel.setWarningSignalCode(SignalCode);
        dataMonitorAlarmModel.setWarningDec(WarningDec);
        dataMonitorAlarmModel.setValue(alarmValue);
        dataMonitorAlarmModel.setType(type);
        dataMonitorAlarmModel.setWarninTime(warninTime);
        return dataMonitorAlarmModel;
    }

    //发送 消息头 header + 消息体 body 封装
    private JsonBkInterfaceModel getJsonBkInterfaceModel(JsonHeaderModel jsonHeaderModel, DataMonitorAlarmModel dataMonitorAlarmModel){
        JsonBkInterfaceModel jsonBkInterfaceModel = new JsonBkInterfaceModel();
        jsonBkInterfaceModel.setHeader(jsonHeaderModel);
        jsonBkInterfaceModel.setBody(dataMonitorAlarmModel);
        return jsonBkInterfaceModel;
    }

    /**
     * 获取UUID唯一标识
     * UUID保证对在同一时空中的所有机器都是唯一的
     * 因为一般数据库主键为String类型，所以接收类型为String，生成的uuid数据包含-，所以要去掉-
     * */
    public static String getUUID(){
        String uuid = UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
        return uuid;
    }

    /**
     * 获取当前系统时间
     *
     * */

    public static String getSystemTimeStampToDate(){
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }
}
